package cn.stylefeng.guns.modular.time.service.impl;

import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import cn.stylefeng.guns.modular.time.entity.Bookmarks;
import cn.stylefeng.guns.modular.time.mapper.BookmarksMapper;
import cn.stylefeng.guns.modular.time.model.request.BookmarksRequest;
import cn.stylefeng.guns.modular.time.service.BookmarksService;

import java.util.Date;

/**
 * 收藏网站表业务实现层
 *
 * @author 叶新华
 * @date 2025/04/18 11:20
 */
@Service
public class BookmarksServiceImpl extends ServiceImpl<BookmarksMapper, Bookmarks> implements BookmarksService {

    @Override
    public void add(BookmarksRequest bookmarksRequest) {
        Bookmarks bookmarks = new Bookmarks();
        // 设置属性
        bookmarks.setName(bookmarksRequest.getName());
        bookmarks.setUrl(bookmarksRequest.getUrl());
        bookmarks.setIcon(bookmarksRequest.getIcon());
        bookmarks.setDescription(bookmarksRequest.getDescription());
        bookmarks.setClickCount(0);
        bookmarks.setUserId(LoginContext.me().getLoginUser().getUserId());
        bookmarks.setDelFlag("N");
        bookmarks.setPinned("N");
        
        // 设置创建信息
        bookmarks.setCreateTime(new Date());
        bookmarks.setCreateUser(LoginContext.me().getLoginUser().getUserId());
        bookmarks.setUpdateTime(new Date());
        bookmarks.setUpdateUser(LoginContext.me().getLoginUser().getUserId());
        
        // 保存
        this.save(bookmarks);
    }

    @Override
    public void edit(BookmarksRequest bookmarksRequest) {
        LambdaQueryWrapper<Bookmarks> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Bookmarks::getBookmarkId, bookmarksRequest.getBookmarkId())
                .eq(Bookmarks::getUserId, LoginContext.me().getLoginUser().getUserId());
        
        Bookmarks bookmarks = this.getOne(queryWrapper);
        if (bookmarks != null) {
            // 设置属性
            bookmarks.setName(bookmarksRequest.getName());
            bookmarks.setUrl(bookmarksRequest.getUrl());
            bookmarks.setIcon(bookmarksRequest.getIcon());
            bookmarks.setDescription(bookmarksRequest.getDescription());
            if (bookmarksRequest.getPinned() != null) {
                bookmarks.setPinned(bookmarksRequest.getPinned());
            }
            
            // 设置更新信息
            bookmarks.setUpdateTime(new Date());
            bookmarks.setUpdateUser(LoginContext.me().getLoginUser().getUserId());
            
            // 更新
            this.updateById(bookmarks);
        }
    }

    @Override
    public PageResult<Bookmarks> findPage(BookmarksRequest bookmarksRequest) {
        LambdaQueryWrapper<Bookmarks> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Bookmarks::getDelFlag, "N")
                .eq(Bookmarks::getUserId, LoginContext.me().getLoginUser().getUserId());

        // 添加搜索条件
        if (bookmarksRequest.getSearchText() != null && !bookmarksRequest.getSearchText().trim().isEmpty()) {
            String searchText = bookmarksRequest.getSearchText().trim();
            queryWrapper.and(wrapper -> wrapper
                .like(Bookmarks::getName, searchText)
                .or()
                .like(Bookmarks::getUrl, searchText)
                .or()
                .like(Bookmarks::getDescription, searchText)
            );
        }

        // 首先按置顶状态降序排序
        queryWrapper.orderByDesc(Bookmarks::getPinned);

        // 添加其他排序条件
        if ("clickCount".equals(bookmarksRequest.getSortField())) {
            queryWrapper.orderBy(true, "desc".equals(bookmarksRequest.getSortOrder()),
                    Bookmarks::getClickCount);
        } else if ("updateTime".equals(bookmarksRequest.getSortField())) {
            queryWrapper.orderBy(true, "desc".equals(bookmarksRequest.getSortOrder()),
                    Bookmarks::getUpdateTime);
        } else {
            // 默认按更新时间倒序排序
            queryWrapper.orderByDesc(Bookmarks::getUpdateTime);
        }

        Page<Bookmarks> page = this.page(new Page<>(bookmarksRequest.getPageNo(), bookmarksRequest.getPageSize()), queryWrapper);
        return PageResultFactory.createPageResult(page);
    }

    @Override
    public void del(BookmarksRequest bookmarksRequest) {
        // 逻辑删除
        LambdaQueryWrapper<Bookmarks> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Bookmarks::getBookmarkId, bookmarksRequest.getBookmarkId())
                .eq(Bookmarks::getUserId, LoginContext.me().getLoginUser().getUserId());
        
        Bookmarks bookmarks = new Bookmarks();
        bookmarks.setDelFlag("Y");
        this.update(bookmarks, queryWrapper);
    }

    @Override
    public void batchDel(BookmarksRequest bookmarksRequest) {
        // 批量逻辑删除
        LambdaQueryWrapper<Bookmarks> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Bookmarks::getBookmarkId, bookmarksRequest.getIds())
                .eq(Bookmarks::getUserId, LoginContext.me().getLoginUser().getUserId());
        
        Bookmarks bookmarks = new Bookmarks();
        bookmarks.setDelFlag("Y");
        this.update(bookmarks, queryWrapper);
    }

    @Override
    public Bookmarks detail(BookmarksRequest bookmarksRequest) {
        LambdaQueryWrapper<Bookmarks> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Bookmarks::getBookmarkId, bookmarksRequest.getBookmarkId())
                .eq(Bookmarks::getUserId, LoginContext.me().getLoginUser().getUserId());
        return this.getOne(queryWrapper);
    }

    @Override
    public void updateClickCount(BookmarksRequest bookmarksRequest) {
        LambdaQueryWrapper<Bookmarks> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Bookmarks::getBookmarkId, bookmarksRequest.getBookmarkId())
                .eq(Bookmarks::getUserId, LoginContext.me().getLoginUser().getUserId());
        
        Bookmarks bookmarks = this.getOne(queryWrapper);
        if (bookmarks != null) {
            bookmarks.setClickCount(bookmarks.getClickCount() + 1);
            bookmarks.setUpdateTime(new Date());
            bookmarks.setUpdateUser(LoginContext.me().getLoginUser().getUserId());
            this.updateById(bookmarks);
        }
    }
}
